﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tracker.Domain.DTOs;
using Tracker.Services.Abstract;
using Tracker.Web.Models;

namespace Tracker.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly IPositionService _positionService;
        private readonly IUserService _userService;

        public HomeController(IPositionService positionService, IUserService userService)
        {
            _positionService = positionService;
            _userService = userService;
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            var user = new UserDTO();
            user.Username = model.Username;
            user.Password = model.Password;
            user = _userService.Login(user);

            if (user != null)
            {
                return RedirectToAction("Positions", new { userID = user.ID });
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password");
                return View(model);
            }
            
        }

        [HttpGet]
        public ActionResult Positions(int userID)
        {
            var positions = _positionService.GetAll(x => x.User.ID == userID);
            return View(positions);
        }

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

		public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
